package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.dao.acs.LinkAccountDao;
import cc.linker.steplink.crm.merchant.pojo.BaseDto;
import cc.linker.steplink.crm.merchant.pojo.BasePageDto;
import cc.linker.steplink.crm.merchant.service.ShopGoodsColumnService;
import cc.linker.steplink.crm.merchant.service.acs.MerchantInfoService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopGoodsColumn")
public class ShopGoodsColumnController {

    @Autowired
    private LinkAccountDao linkAccountDao;
    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private ShopGoodsColumnService shopGoodsColumnService;
    @Autowired
    private UserInfoUtil userInfoUtil;



    /**
     * 添加商品专栏
     */
    @PostMapping("/save")
    public BaseDto save(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        BaseDto dto = new BaseDto();
        Date date = new Date();
        params.put("createTime", date);
        params.put("updateTime", date);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        params.put("orgId", orgId);
        String customerId = linkAccountDao.getCustomerIdByAdminId(adminId);
        params.put("customerId", customerId);
        if (shopGoodsColumnService.save(params)) {
            dto.setRt(1);
            dto.setDes("商品专栏保存成功");
        } else {
            dto.setRt(0);
            dto.setDes("商品专栏保存失败");
        }
        return dto;
    }

    /**
     * 更新专栏
     * Status：更新值
     * Type：更新类型 0：更新状态(COLUMN_STATUS 1.更新中 2.已完结) 1：删除 2：更新商品状态(GOODS_STATUS 0 待上架 1 上架中 2 已下架)
     */
    @PostMapping("/updateStatus")
    public BaseDto updateStatus(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String adminId = userinfo.getAdminId();
        String orgId = userinfo.getOrgId();
        Boolean ifSales = userinfo.getSaler();
        BaseDto dto = new BaseDto();

        boolean flag = true;
        if(StringUtils.isEmpty(params.get("columnId"))){
            throw GlobalException.build("至少选择一个专栏");
        }else{
            String columnIds = params.get("columnId").toString();
            String[] split = columnIds.split(",");
            for(String id:split){
                Long columnId = Long.valueOf(id);
                Integer status = (Integer) params.get("status");
                Integer type = (Integer) params.get("type");
                boolean b = shopGoodsColumnService.updateColumnStatus(columnId, status, type, adminId, orgId, ifSales);
                if(flag && !b){
                    flag = false;
                }
            }
        }
        if(flag){
            dto.setRt(1);
            dto.setDes("更新成功");
        }else{
            dto.setRt(0);
            dto.setDes("更新失败");
        }
        return dto;

    }

    /**
     * 获取商品专栏列表
     */
    @PostMapping("/getList")
    public Object getList(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Boolean ifSales = userinfo.getSaler();
        if (ifSales) {
            // 联盟商家
            String adminId = userinfo.getAdminId();
            String customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
            params.put("belongId", customerId);
        } else {
            // 联盟大麦
            params.put("belongId", "");
        }
        String orgId = userinfo.getOrgId();
        params.put("orgId", orgId);
        BasePageDto<Map<String, Object>> dto = new BasePageDto<Map<String, Object>>();
        Integer perPage = (Integer) params.get("perPage");
        Integer pageIndex = (Integer) params.get("pageIndex");
        Integer start = (pageIndex - 1) * perPage;
        Integer total = shopGoodsColumnService.getShopGoodsColumnListCount(params);
        params.put("start", start);
        List<Map<String, Object>> map = shopGoodsColumnService.getShopGoodsColumnList(params);
        dto.setCon(map);
        dto.setCurrentPage(pageIndex);
        dto.setCount(total);
        dto.setPerPage((Integer) params.get("perPage"));
        dto.setTotalPage((total % perPage == 0) ? (total / perPage) : (total / perPage + 1));
        return dto;
    }

    /**
     * 获取专栏、商品关联信息
     */
    @GetMapping("/getDetail")
    public Object getDetail(Long columnId, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Boolean ifSales = userinfo.getSaler();
        String belongId = "";
        if (ifSales != null && ifSales) {
            // 联盟商家
            String adminId = userinfo.getAdminId();
            String customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
            belongId = customerId;
        }
        Map<String, Object> map = shopGoodsColumnService.getShopGoodsColumnDetail(columnId, belongId);
        return map;
    }

    /**
     * 添加专栏、内关联
     */
    @PostMapping("/AddCloumnContent")
    public BaseDto AddCloumnContent(@RequestBody Map<String, Object> params) {
        BaseDto dto = new BaseDto();
        if (shopGoodsColumnService.AddCloumnContent(params)) {
            dto.setRt(1);
            dto.setDes("更新成功");
        } else {
            dto.setRt(0);
            dto.setDes(params.get("msg").toString());
        }
        return dto;
    }

    /**
     * 删除专栏、内容关联
     */
    @PostMapping("/DeleteCloumnContent")
    public BaseDto DeleteCloumnContent(@RequestBody Map<String, Object> params) {
        BaseDto dto = new BaseDto();
        if (shopGoodsColumnService.DeleteCloumnContent(params)) {
            dto.setRt(1);
            dto.setDes("删除成功");
        } else {
            dto.setRt(0);
            dto.setDes("删除失败");
        }
        return dto;
    }

    /**
     * 获取所有专栏
     */
    @PostMapping("/getAllList")
    public List<Map<String, Object>> getAllList(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> params = new HashMap<>();
        Boolean ifSales = userinfo.getSaler();
        if (ifSales) {
            // 联盟商家
            String adminId = userinfo.getAdminId();
            String customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
            params.put("belongId", customerId);
        } else {
            // 联盟大麦
            params.put("belongId", "");
        }
        String orgId = userinfo.getOrgId();
        params.put("orgId", orgId);
        return shopGoodsColumnService.getAllList(params);
    }
}